package com.lyy.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.concurrent.TimeUnit;

/**
 * @author JoeZhou
 */
public class GuavaUtil {

    /**
     * 使用静态内部类保证单例
     */
    private static final class CacheHolder {
        /**
         * 10分钟的缓存实例
         */
        private static final Cache<String, Object> tenMinutesCache = CacheBuilder.newBuilder()
                .initialCapacity(10)
                .maximumSize(100)
                .concurrencyLevel(5)
                .expireAfterWrite(600, TimeUnit.SECONDS)
                .recordStats()
                .build();
        /**
         * 30分钟的缓存实例
         */
        private static final Cache<String, Object> halfHourCache = CacheBuilder.newBuilder()
                .initialCapacity(10)
                .maximumSize(100)
                .concurrencyLevel(5)
                .expireAfterWrite(1800, TimeUnit.SECONDS)
                .recordStats()
                .build();
    }

    /**
     * 获取一个10分钟后过期的Cache实例
     *
     * @return 一个10分钟后过期的Cache实例
     */
    public static Cache<String, Object> getTenMinutesCache() {
        return CacheHolder.tenMinutesCache;
    }

    /**
     * 获取一个30分钟后过期的Cache实例
     *
     * @return 一个30分钟后过期的Cache实例
     */
    public static Cache<String, Object> getHalfHourCache() {
        return CacheHolder.halfHourCache;
    }
}
